package com.wuyou.simple;


import java.util.Arrays;

/**
 * 数组中两元素的最大乘积
 */
public class MaxProduct {

    public static void main(String[] args) {
        int[] nums = {3,4,5,2};
        System.out.println(solution_4(nums));
    }

    public static int solution_4(int[] nums) {
        int a = nums[0], b = nums[1];
        if (a < b) {
            int temp = a;
            a = b;
            b = temp;
        }

        for (int i = 2; i < nums.length; i++) {
            int temp = nums[i];
            if (temp > a) {
                b = a;
                a = temp;
            } else if (temp > b) {
                b = temp;
            }
        }
        return (a - 1) * (b - 1);
    }


    public static int solution_3(int[] nums) {
        int[] maxNum = new int[2];
        int tempIndex = 0;
        for (int i = 0; i < nums.length; i++) {
            if (maxNum[0] < nums[i] - 1) {
                maxNum[0] = nums[i] - 1;
                tempIndex = i;
            }
        }
        nums[tempIndex] = -999;

        for (int i = 0; i < nums.length; i++) {
            maxNum[1] = Math.max(maxNum[1], nums[i] - 1);
        }
        return maxNum[0] * maxNum[1];
    }

    public static int solution_2(int[] nums) {
        Arrays.sort(nums);
        return (nums[nums.length - 1] - 1) * (nums[nums.length - 2] - 1);
    }

    public static int solution_1(int[] nums) {
        int maxRes = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                int temp = (nums[i] - 1) * (nums[j] - 1);
                maxRes = Math.max(maxRes, temp);
            }

        }
        return maxRes;
    }
}
